<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class YTuongService {

    public static function getMaxDiemSangTao() {
        $YT = new vwYTuong();
        $Criteria_max = new CDbCriteria();
        $Criteria_max->select = 'max(DiemSangTao) AS maxSangTao';
        $YTuong = $YT->model()->find($Criteria_max);
        return vwYTuong::model()->findByAttributes(array('DiemSangTao' => $YTuong['maxSangTao']));
    }

    public static function getById($Id) {
        return VwYtuong::model()->findByAttributes(array('Id' => $Id));
    }

    public static function getYTuongs() {
        return VwYtuong::model()->findAll();
    }

    public static function getYTuongById($id) {
        return YTuong::model()->findByPk($id);
    }

    public static function getYTuongsByNguoiDungId($Id) {
        return VwYtuong::model()->findAllByAttributes(array('NguoiDang' => $Id));
    }

    public static function getYTuongsByDanhMucs($DanhMucs) {
        if ($DanhMucs == '') {
            return VwYtuong::model()->findAll();
        }
        $dm = '';
        foreach ((array) $DanhMucs as $DanhMuc) {
            $dm .= $DanhMuc . ",";
        }
        if (strlen($dm) > 0 && $dm[strlen($dm) - 1] == ',') {
            $dm = substr($dm, 0, strlen($dm) - 1);
        }
        $sql = 'SELECT * FROM vw_ytuong where Id in (select DISTINCT YTuongId from ytuong_danhmuc where DanhMucId in (' . $dm . ')) ORDER BY DiemSangTao DESC';
        return VwYtuong::model()->findAllBySql($sql);
    }

    public static function removeYTuongById($Id) {
        $DanhMuc = YTuong_Danhmuc::model()->findAllByAttributes(array('YTuongId' => $Id));
        foreach ($DanhMuc as $DM) {
            $DM->delete();
        }
//        $YTuong = YTuong::model()->findByPk($Id);
        return YTuong::model()->deleteByPk($Id);
    }

    public static function ToggleSeeYTuong($Id) {
        $YTuong = YTuong::model()->findByPk($Id);
        $YTuong->TrangThai == 1 ? $YTuong->TrangThai = 0 : $YTuong->TrangThai = 1;
        $YTuong->update();
        return $YTuong->TrangThai;
    }

    public static function ThemLuotXem($Id) {
        $YT = YTuong::model()->findByPk($Id);
        $YT->LuotXem ++;
        $YT->save();
    }

    public static function saveChangesArticle($Id, $Content) {
        $YT = YTuong::model()->findByPk($Id);
        $YT->NoiDung = $Content;
        return $YT->save();
    }

    public static function addComment($Id, $content) {
        $cm = new Comment();
        $cm->YTuongId = $Id;
        $cm->NguoiGopY = Yii::app()->user->id;
        $cm->NoiDung = $content;
        $cm->NgayDang = date('Y-m-d H:i:s');
        return $cm->save();
    }

    public static function addRepComment($Id, $content) {
        $rep = new RepComment();
        $rep->GopYId = $Id;
        $rep->NoiDung = $content;
        $rep->NgayDang = date('Y-m-d H:i:s');
        $rep->NguoiTraLoi = Yii::app()->user->id;
        return $rep->save();
    }

    public static function getComment($YTuongId) {
        $res = array();
        $YTuong = YTuong::model()->findByPk($YTuongId);
        if ($YTuong != null) {
            $comments = $YTuong->getRelated('gopies');
        } else {
            return $res;
        }
        foreach ($comments as $comment) {
            $commentObject = array();
            $commentRelated = array();
            $RepComments = $comment->getRelated('traloigopies');
            $RepPerson = $comment->getRelated('nguoiGopY');

            foreach ($RepComments as $RepComment) {
                $RepCommentObject = array();
                $RepCommentObject['content'] = $RepComment;
                $RepCommentObject['persion'] = $RepComment->getRelated('nguoiTraLoi');
                $commentRelated[$RepComment->Id] = $RepCommentObject;
            }
            $commentObject['content'] = $comment;
            $commentObject['persion'] = $RepPerson;
            $commentObject['relate'] = $commentRelated;
            $res[$comment->Id] = $commentObject;
        }
        return $res;
    }

    public static function searchArticle($content, $target, $sort) {
        $match = addcslashes($content, '%_'); // escape LIKE's special characters
        $tar = $target == 'all' ? 'TieuDe LIKE :match or TenNguoiDang LIKE :match' : $target == 'title' ? 'TieuDe LIKE :match' : 'TenNguoiDang LIKE :match';
        $sor = $sort == 'time' ? 'NgayDang DESC' : $sort == 'view' ? 'LuotXem DESC' : $sort == 'share' ? 'LuotChiaSe DESC' : 'DiemSangTao DESC';
        $q = new CDbCriteria(array(
            'condition' => $tar, // no quotes around :match
            'order' => $sor,
            'params' => array(':match' => "%$match%")  // Aha! Wildcards go here
        ));
        return vwYTuong::model()->findAll($q);
    }

    public static function searchArticleByAuthor($content) {
        $match = addcslashes($content, '%_'); // escape LIKE's special characters
        $q = new CDbCriteria(array(
            'condition' => "TenNguoiDang LIKE :match", // no quotes around :match
            'params' => array(':match' => "%$match%")  // Aha! Wildcards go here
        ));
        return VwYtuong::model()->findAll($q);
    }

    public static function getDanhMuc($IdLinhVuc) {
        $user_danhmuc = NguoiDungService::getDanhMucIds($IdLinhVuc);

        $Danhmucs = $IdLinhVuc != null ? DanhMuc::model()->findAllByAttributes(array('LinhVucId' => $IdLinhVuc)) : DanhMuc::model()->findAll();
        $result = array();
        foreach ($Danhmucs as $danhmuc) {
            $key = in_array($danhmuc->Id, $user_danhmuc) ? '_' . $danhmuc->Id : $danhmuc->Id;
            $result[$key] = $danhmuc->Ten;
        }
        return $result;
    }

}
